﻿Console.WriteLine("猜数字游戏开始！");
Console.WriteLine("输入 exit 可退出游戏");

Random random = new Random();
int secretNumber = random.Next(1, 101); // 1-100
int attempts = 0;

while (true)
{
    Console.Write("\n请输入你的猜测（1~100）：");
    string? input = Console.ReadLine();

    if (input?.ToLower() == "exit") break;

    if (string.IsNullOrWhiteSpace(input) || !int.TryParse(input, out int guess))
    {
        Console.WriteLine("无效输入，请输入一个有效的整数！");
        continue;
    }

    attempts++;

    if (guess == secretNumber)
    {
        Console.WriteLine($"恭喜你猜对了！共猜了 {attempts} 次。");
        break;
    }
    else if (guess > secretNumber)
    {
        Console.WriteLine($"太大了，这是你第 {attempts} 次猜错！");
    }
    else
    {
        Console.WriteLine($"太小了，这是你第 {attempts} 次猜错！");
    }
}

Console.WriteLine("游戏结束，感谢游玩！");